package com.public_module.basemvp.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import com.public_module.R;
import com.public_module.basemvp.BaseMvp;
import com.public_module.basemvp.model.Model;
import com.public_module.basemvp.present.BasePresenter;
import com.public_module.basemvp.view.BaseView;
import com.public_module.dialogs.WaitDialog;
import com.public_module.tools.ToastUtils;
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle2.LifecycleProvider;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public abstract class BaseFragment<M extends Model, V extends BaseView, P extends BasePresenter> extends Fragment implements BaseMvp<M, V, P>, BaseView {
    protected String TAG = "BaseFragment";
    protected WaitDialog waitDialog;
    protected P presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        waitDialog = new WaitDialog(getActivity(), R.style.WaitDialog);
        WindowManager.LayoutParams lp = waitDialog.getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        //创建
        presenter = createPresenter();
        if (presenter != null) {
            presenter.registerModel(createModel());
            presenter.registerView(createView());
        }
        LifecycleProvider<Lifecycle.Event> lifecycleProvider = AndroidLifecycle.createLifecycleProvider(this);
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnDispose(new Action() {
                    @Override
                    public void run() throws Exception {
                    }
                })
                .compose(lifecycleProvider.<Long>bindUntilEvent(Lifecycle.Event.ON_PAUSE))
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long num) throws Exception {
                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(waitDialog!=null)
        {
            waitDialog.dismiss();
            waitDialog=null;
        }
        presenter.destroy();
    }

    @Override
    public void showProgress() {
        waitDialog.show();
    }

    @Override
    public void hideProgress() {
            waitDialog.dismiss();
    }

    @Override
    public void showMsg(String msg) {
        ToastUtils.showLong(getActivity(), msg);
    }

    protected void intentToActivityWithoutParameter(Context context, Class clazz) {
        Intent intent = new Intent();
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void intentToActivityWithIntParameter(Context context, Class clazz, String key, int value) {
        Intent intent = new Intent();
        intent.putExtra(key, value);
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void intentToActivityWithStringParameter(Context context, Class clazz, String key, String str) {
        Intent intent = new Intent();
        intent.putExtra(key, str);
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void intentToActivityWithPParameter(Context context, Class clazz, String key, Parcelable object) {
        Intent intent = new Intent();
        intent.putExtra(key, object);
        intent.setClass(context, clazz);
        startActivity(intent);
    }

    protected void intentToActivityWithSParameter(Context context, Class clazz, String key, Serializable object) {
        Intent intent = new Intent();
        intent.putExtra(key, object);
        intent.setClass(context, clazz);
        startActivity(intent);
    }
}
